package panel.weatherDetail
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.Scale9Grid;
	import assists.TextFieldUtils;
	import assists.TimeToStr;
	import assists.URI;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	
	import struct.Weather;
	
	public class WeatherBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		public var info:Weather;
		public var id:int = 0;
		
		private var back:Scale9Grid;
		private var weatherImg1:ImageResource;
		private var weatherImg2:ImageResource;
		
		private var dateText:TextField;
		private var infoText:TextField;
		private var windText:TextField;
		private var tempText:TextField;
		
		public function WeatherBox(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			info = new Weather;
			
			initThis();
		}
		
		private function initThis():void
		{
			var img:Bitmap = new _loader.uiManager.weatherBack as Bitmap;
			back = new Scale9Grid(img.bitmapData,5,5);
			
			dateText = new TextField;
			dateText.mouseEnabled = true;
			dateText.selectable = false;
			dateText.wordWrap = false;
			dateText.multiline = false;
			
			infoText = new TextField;
			infoText.mouseEnabled = true;
			infoText.selectable = false;
			infoText.wordWrap = false;
			infoText.multiline = false;
			
			windText = new TextField;
			windText.mouseEnabled = true;
			windText.selectable = false;
			windText.wordWrap = false;
			windText.multiline = false;
			
			tempText = new TextField;
			tempText.mouseEnabled = true;
			tempText.selectable = false;
			tempText.wordWrap = false;
			tempText.multiline = false;
		}
		
		public function changeData(obj:Weather):void
		{
			info = obj;
			
			if(id == 0){
				showBig();
			}else{
				showSmall();
			}
		}
		
		private function showSmall():void
		{
			back.width = _stage.stageWidth * .9;
			back.height = _stage.stageHeight * .25;
			this.addChild(back);
			
			dateText.x = _stage.stageWidth * .01;
			dateText.y = _stage.stageHeight * .01;
			dateText.width = _stage.stageWidth * .8;
			TextFieldUtils.setHtmlText(dateText,HtmlText.formatSizeColor("<b>" + TimeToStr.formatYearAndDayInChinese(info.date) + "    " + info.city + "</b>",0x000000,20),true);
			this.addChild(dateText);
			
			tempText.x = _stage.stageWidth * .01;
			tempText.y = _stage.stageHeight * .08;
			tempText.width = _stage.stageWidth * .48;
			TextFieldUtils.setHtmlText(tempText,HtmlText.formatSizeColor("<b>" + info.temp + "</b>",0x000000,18),true);
			this.addChild(tempText);
			
			
			infoText.x = _stage.stageWidth * .01;
			infoText.y = _stage.stageHeight * .15;
			infoText.width = _stage.stageWidth * .48;
			TextFieldUtils.setHtmlText(infoText,HtmlText.formatSizeColor("<b>" + info.info + "</b>",0x000000,18));
			this.addChild(infoText);
			
			TextFieldUtils.setHtmlText(windText,HtmlText.formatSizeColor("<b>" + info.wind + "</b>",0x000000,18));
			windText.width = windText.textWidth + 4;
			windText.x = _stage.stageWidth * .85 - windText.width;
			windText.y = _stage.stageHeight * .15;
			this.addChild(windText);
			
			weatherImg1 = new ImageResource(_loader,"http://m.weather.com.cn/img/b" + info.imgUrl1 + ".gif",addPic1);
			weatherImg2 = new ImageResource(_loader,"http://m.weather.com.cn/img/b" + info.imgUrl2 + ".gif",addPic2);
			
			function addPic1():void{
				addChild(weatherImg1);
				weatherImg1.x = _stage.stageWidth * .51;
				weatherImg1.y = _stage.stageHeight * .05;
				weatherImg1.width = _stage.stageWidth * .1;
				weatherImg1.height = weatherImg1.width;
			}
			function addPic2():void{
				addChild(weatherImg2);
				weatherImg2.x = _stage.stageWidth * .71;
				weatherImg2.y = _stage.stageHeight * .05;
				weatherImg2.width = _stage.stageWidth * .1;
				weatherImg2.height = weatherImg2.width;
			}
		}
		
		private function showBig():void
		{
			back.width = _stage.stageWidth * .9;
			back.height = _stage.stageHeight * .45;
			this.addChild(back);
			
			dateText.x = _stage.stageWidth * .01;
			dateText.y = _stage.stageHeight * .01;
			dateText.width = _stage.stageWidth * .8;
			TextFieldUtils.setHtmlText(dateText,HtmlText.formatSizeColor("<b>" + TimeToStr.formatYearAndDayInChinese(info.date) + "    " + info.city + "</b>",0x000000,20),true);
			this.addChild(dateText);
			
			tempText.x = _stage.stageWidth * .01;
			tempText.y = _stage.stageHeight * .08;
			tempText.width = _stage.stageWidth * .48;
			TextFieldUtils.setHtmlText(tempText,HtmlText.formatSizeColor("<b>" + info.temp + "</b>",0x000000,18),true);
			this.addChild(tempText);
			
			
			infoText.x = _stage.stageWidth * .01;
			infoText.y = _stage.stageHeight * .15;
			infoText.width = _stage.stageWidth * .48;
			TextFieldUtils.setHtmlText(infoText,HtmlText.formatSizeColor("<b>" + info.info + "</b>",0x000000,18));
			this.addChild(infoText);
			
			TextFieldUtils.setHtmlText(windText,HtmlText.formatSizeColor("<b>" + info.wind + "</b>",0x000000,18));
			windText.width = windText.textWidth + 4;
			windText.x = _stage.stageWidth * .85 - windText.width;
			windText.y = _stage.stageHeight * .15;
			this.addChild(windText);
			
			weatherImg1 = new ImageResource(_loader,"http://m.weather.com.cn/img/b" + info.imgUrl1 + ".gif",addPic1);
			weatherImg2 = new ImageResource(_loader,"http://m.weather.com.cn/img/b" + info.imgUrl2 + ".gif",addPic2);
			
			function addPic1():void{
				addChild(weatherImg1);
				weatherImg1.x = _stage.stageWidth * .51;
				weatherImg1.y = _stage.stageHeight * .05;
				weatherImg1.width = _stage.stageWidth * .1;
				weatherImg1.height = weatherImg1.width;
			}
			function addPic2():void{
				addChild(weatherImg2);
				weatherImg2.x = _stage.stageWidth * .71;
				weatherImg2.y = _stage.stageHeight * .05;
				weatherImg2.width = _stage.stageWidth * .1;
				weatherImg2.height = weatherImg2.width;
			}
			
			var suggestText:TextField = new TextField;
			suggestText.mouseEnabled = true;
			suggestText.selectable = false;
			suggestText.wordWrap = true;
			suggestText.multiline = true;
			suggestText.x = _stage.stageWidth * .01;
			suggestText.y = _stage.stageHeight * .25;
			suggestText.width = _stage.stageWidth * .88;
			suggestText.height = _stage.stageHeight * .19;
			
			if(info.suggest != null){
				TextFieldUtils.setHtmlText(suggestText,HtmlText.formatSizeColor("<b>" + info.suggest + "</b>",0x000000,18),true);
			}else{
				TextFieldUtils.setHtmlText(suggestText,HtmlText.formatSizeColor("<b>" + "暂时没有建议" + "</b>",0x000000,18),true);
			}
			this.addChild(suggestText);
		}
		
		public override function get height():Number
		{
			return (id == 0)?(_stage.stageHeight * .45):(_stage.stageHeight * .25);
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				back = null;
				weatherImg1 = null;
				weatherImg2 = null;
			}
		}
	}
}